<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product_model extends OC_Model {

	public function get_items($limit,$offset,$cat_id)
	{
		$this->db->select('a.*,b.cp_name,b.cp_id');
		$this->db->from('products a');
		$this->db->join('category_product b', 'b.cp_id = a.cp_id', 'left');
		if(!empty($cat_id)) $this->db->where('a.cp_id', $cat_id);
		if(!empty($limit)) $this->db->limit($limit,$offset);
		return $this->db->get()->result();
	}

	public function get_sub_items($cat_id)
	{
		$this->db->from('sub_category_product');
		$this->db->where('cp_id', $cat_id);
		return $this->db->get()->result();
	}

	public function get_arr_items($cat_id,$sub_cat_id)
	{
		$this->db->from('products');
		$this->db->where('cp_id', $cat_id);
		$this->db->where('scp_id', $sub_cat_id);
		return $this->db->get()->result();
	}
    
    public function count($limit=false,$offset=false,$cat_id=false)
    {
        return count($this->get_items($limit,$offset,$cat_id));
    }


	public function get_categories()
	{
		$this->db->order_by('cp_order', 'Asc');
		$this->db->where('cp_active', 1);
		return $this->db->get('category_product')->result();
	}

	public function get_images($p_id=false)
	{
		$this->db->where('pi_p_id', $p_id);
		return $this->db->get('product_images')->result();
	}

	public function get_item_details($id=false){
		$this->db->select('a.*,b.cp_name,b.cp_id');
		$this->db->from('products a');
		$this->db->join('category_product b', 'b.cp_id = a.cp_id', 'left');
		$this->db->where('a.p_id', $id);
		return $this->db->get()->row_array();
	}

	public function item_per_category($cat,$limit,$offset)
	{
		$this->db->select('a.*,b.cp_name,b.cp_id');
		$this->db->from('products a');
		$this->db->join('category_product b', 'b.cp_id = a.cp_id', 'left');
		$this->db->where('a.cp_id', $cat);
		if(!empty($limit)) $this->db->limit($limit,$offset);
		return $this->db->get()->result();
	}

	public function get_meta($meta=false,$id=false)
	{
		$this->db->select('p_meta_'.$meta);
		$this->db->where('p_id', $id);
		$meta_data = $this->db->get('products')->row_array();
		return $meta_data['p_meta_'.$meta];
	}

}

/* End of file product_model.php */
/* Location: ./applications/module_front/products/models/product_model.php */